/** 
 * This js file create in ShaoXing ZheJian
 * author : jaypha
 * QQ : psufool@qq.com
 * homePage : www.psufool.cn
 * email : jaypha@psufool.cn
 */

/**
 * 对话框
 * @param dialogWidth		对话框宽度
 * @param dialogHeight		对话框高度
 * @param dialogTitle		对话框标题
 * @param dialogContent		对话框内容
 * @param focuseObj			单击确定后获取焦点对象
 */
var subMess = function(dialogWidth,dialogHeight,dialogTitle,dialogContent,focuseObj){
	var subMessDialog = new dg.curWin.$.dialog({
			id:'subMess',
			width:dialogWidth,
			height:dialogHeight,
			parent:dg,
			cover: true,
			iconTitle:false,
			xButton:false,
			resize:false,
			rang:true,
			maxBtn:false,
			autoPos:{left:'center',top:'center'},
			cancelBtnTxt:'确定',
			onCancel:function(){
				focuseObj.focus();
			},
			title:dialogTitle,
			html:'<p class="mess-detail">' + dialogContent + '</p>'
	});
	subMessDialog.ShowDialog();
}

/**
 * 鼠标经过菜单
 */
var menuOver = function() {
	$(this).addClass("cur");
	var isHaveSub = false;
	if ($(this).find("ul").length > 0) {
		isHaveSub = true;
	}
	if (isHaveSub) {
		$(this).find("ul").show();
	}
}

/**
 * 鼠标离开菜单
 * 
 * @return
 */
var menuOut = function() {
	$(this).removeClass("cur");
	var isHaveSub = false;
	if ($(this).find("ul").length > 0) {
		isHaveSub = true;
	}
	if (isHaveSub) {
		$(this).find("ul").hide();
	}
}

/**
 * 单击菜单项
 * 
 * @return
 */
var menuClick = function() {
	var index = $(this).parent().children().index(this);
	defaultFid = $('#defaultFid').val();
	switch (index) {
	case 1:
		var idValue = $(".list input[name='id[]']:checked").val();
		editItem(idValue, defaultFid);
		break;
	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	case 5:
		var lockedScoll = false;
		if($.browser.msie){
			lockedScoll = true;
		}else if($.browser.mozilla){
			lockedScoll = false;
		}else{
			//searchWidth += 10;
			//searchHeight += 1;
		}
		var searchMess = new $.dialog( {
			id : 'searchMess',
			width : searchWidth,
			height : searchHeight,
			iconTitle : false,
			resize : false,
			cover : true,
			drag : true,
			rang : true,
			maxBtn : false,
			lockScroll : lockedScoll,
			autoPos : {left : 'center',top : 'center'},
			cancelBtnTxt : '搜 索',
			onCancel : function() {
				$('#listForm .apptend-container').html('');
				$("#active").val("search");
				$(searchMess.iDG('searchMess')).find('.search-box').appendTo("#listForm .apptend-container");
				$("#listForm").submit();
			},
			title : '高级搜索',
			html : '<div class="search-box" id="search-box">' + $('.searchBox').html() + '</div>'
		});
		searchMess.ShowDialog();
		break;
	default:
		editItem("", defaultFid);
		break;
	}
}

/**
 * 鼠标经过项目
 * 
 * @return
 */
var itemOver = function() {
	$(this).addClass("over");
	$(this).find(".single-menu").show();
	try{
		if (permission[perm].edit) {
			$(this).find('.edit').show();
		} else {
			$(this).find('.edit').hide();
		}
	}catch(e){
		$(this).find('.edit').hide();
	}
	try{
		if (permission[perm].del) {
			$(this).find('.del').show();
		} else {
			$(this).find('.del').hide();
		}
	}catch(e){
		$(this).find('.del').hide();
	}
}

/**
 * 鼠标离开项目
 * 
 * @return
 */
var itemOut = function() {
	$(this).removeClass("over");
	$(this).find(".single-menu").hide();
}

/**
 * 项目单击
 * 
 * @return
 */
var itemClick = function() {
	if ($(this).find(".id-checkbox").attr("checked")) {
		$(this).find(".id-checkbox").removeAttr("checked");
		$(this).removeClass("select");
	} else {
		$(this).find(".id-checkbox").attr("checked", true);
		$(this).addClass("select");
	}
	checkAll();
	selectMenu();
}

/**
 * ID input 单击
 * 
 * @param event
 * @return
 */
var idClick = function(event) {
	event.stopPropagation();
	if ($(this).attr("checked")) {
		$(this).parents('.item').addClass("select");
	} else {
		$(this).parents('.item').removeClass("select");
	}
	checkAll();
	selectMenu();
}

/**
 * 全选复选框单击
 * 
 * @return
 */
var allClick = function() {
	if ($(this).attr("checked")) {
		$(".id-checkbox").attr("checked", true);
		$(".main > .list > ul form .item").addClass("select");
	} else {
		$(".id-checkbox").removeAttr('checked');
		$(".main > .list > ul form .item").removeClass("select");
	}
	selectMenu();
}

/**
 * 获取已经ID长度
 * 
 * @return
 */
var selectedIdLength = function() {
	return $(".id-checkbox:checked").length;
}

/**
 * 全选
 * 
 * @return
 */
var checkAll = function() {
	if (selectedIdLength() == $(".id-checkbox").length) {
		$("input[name='all']").attr("checked", true);
	} else {
		$("input[name='all']").removeAttr('checked');
	}
}

/**
 * 加载主菜单
 * 
 * @return
 */
var loadMainMenu = function() {
	var menuList = $.ajax( {
		async : false,
		url : getInfo + "/getMainMenu"
	}).responseText;
	if ($.browser.msie) {
		if ($.browser.version == "6.0") {
			menuList = menuList.replace(/javascript:;/ig, "#");
		}
	}
	$(".position > ul").html(menuList);
	loadDelSubMenu();
	loadMoveSubMenu();
	getDefaultMenu();
}

/**
 * 加载删除的二级菜单
 */
var loadDelSubMenu = function() {
	var mainDelSubMenu = $.ajax( {
		async : false,
		url : getInfo + "/getMainSubDelMenu"
	}).responseText;
	$(".main > .position > ul > .menu_del").append(mainDelSubMenu).find("ul > li > a").bind('click', delMenuClick);
}

/**
 * 加载批量操作的二级菜单
 */
var loadMoveSubMenu = function() {
	$(".main > .position > ul .menu_move > a").attr("title", "批量操作").html("批量操作");
	var mainMoveSubMenu = $.ajax( {
		async : false,
		url : getInfo + "/getMainSubMenu"
	}).responseText;
	$(".main > .position > ul > .menu_move").append(mainMoveSubMenu).find("ul > li > a").bind('click',batchMenuClick);
}

/**
 * 显示菜单选项
 */
var selectMenu = function() {
	var objMenu = $(".main > .position > ul > li");

	/**
	 * 0、添加 1、修改 2、删除 3、批量操作 4、回复 5、搜索
	 */

	if (selectedIdLength() > 0) {
		if (selectedIdLength() == 1) {
			try{
				if (permission[perm].add) {
					objMenu.eq(0).show();
				} else {
					objMenu.eq(0).hide();
				}
			}catch(e){
				objMenu.eq(0).hide();
			}
			try{
				if (permission[perm].edit) {
					objMenu.eq(1).show();
				} else {
					objMenu.eq(1).hide();
				}
			}catch(e){
				objMenu.eq(2).hide();
			}
			try{
				if (permission[perm].del) {
					objMenu.eq(2).show();
				} else {
					objMenu.eq(2).hide();
				}
			}catch(e){
				objMenu.eq(2).hide();
			}
			try{
				if (permission[perm].move) {
					objMenu.eq(3).show();
				} else {
					objMenu.eq(3).hide();
				}
			}catch(e){
				objMenu.eq(3).hide();
			}
		} else {
			objMenu.eq(1).hide();
			objMenu.eq(4).hide();
			try{
				if (permission[perm].del) {
					objMenu.eq(2).show();
				} else {
					objMenu.eq(2).hide();
				}
			}catch(e){
				objMenu.eq(2).hide();
			}
			try{
				if (permission[perm].move) {
					objMenu.eq(3).show();
				} else {
					objMenu.eq(3).hide();
				}
			}catch(e){
				objMenu.eq(3).hide();
			}
			try{
				if (permission[perm].search) {
					objMenu.eq(5).show();
				} else {
					objMenu.eq(5).hide();
				}
			}catch(e){
				objMenu.eq(5).hide();
			}
		}
	} else {
		getDefaultMenu();
	}
}

/**
 * 获取默认状态下菜单选项
 */
var getDefaultMenu = function() {
	var objMenu = $(".main > .position > ul > li");
	try{
		if (permission[perm].add) {
			objMenu.eq(0).show();
		} else {
			objMenu.eq(0).hide();
		}
	}catch(e){
		objMenu.eq(0).hide();
	}
	objMenu.eq(1).hide();
	objMenu.eq(2).hide();
	objMenu.eq(3).hide();
	objMenu.eq(4).hide();
	try{
		if (permission[perm].search) {
			objMenu.eq(5).show();
		} else {
			objMenu.eq(5).hide();
		}
	}catch(e){
		objMenu.eq(5).hide();
	}
}

/**
 * 单条记录菜单选项命令
 */
var singleMenu = function(event) {
	event.stopPropagation();
	var index = $(this).parent().children().index(this);
	var thisId = $(this).parents(".item").find(".id-checkbox").val();
	switch (index) {
	case 1:
		var idValue = $(this).parents(".item").find(".id-checkbox").val();
		delF(idValue);
		break;
	default:
		var idValue = $(this).parents(".item").find(".id-checkbox").val();
		editItem(idValue);
		break;
	}
	return false;
}

/**
 * 更改排序索引
 */
var changeIndex = function() {
	var oIndexValue = $(this).val();
	var idValue = $(this).parents(".item").find(".id-checkbox").val();
	var updateUrl = getInfo + "/updateOIndex/" + tvName + "/" + idValue + "/"
			+ oIndexValue;
	var returnValue = $.ajax( {
		async : false,
		url : updateUrl
	}).responseText.toLowerCase();
}

/**
 * 修改项
 * 
 * @param id
 * @return
 */
var editItem = function(id) {
	var obj = $(".id-checkbox[value=" + id + "]");
	var edit_url = obj.parents('.item').find('a').eq(0).attr('href');
	var editId = "editId";
	var title = "";
	if (id.length == 0) {
		title = addTitle;
	} else {
		title = editTitle;
	}
	var editUrl = editPage;
	if(edit_url==undefined || edit_url=='#' || edit_url.length==0){
		editUrl += '/' + id;
	}else{
		editUrl = edit_url;
	}
	try {
		editMess.cancel();
	} catch (e) {}
	if($.browser.msie){
		var lockedScroll = true;
	}else if($.browser.mozilla){
		var lockedScroll = false;
	}else if($.browser.safari){
		var lockedScroll = false;
		//editHeight += -0.5;
	}else{
		editHeight += 1;
	}
	var editMess = new $.dialog({
		id:'editMess',
		width:editWidth,
		height:editHeight,
		title:title,
		iconTitle:false,
		maxBtn:true,
		minBtn:false,
		xButton:true,
		drag:true,
		cover:true,
		rang:true,
		resize:false,
		cancelBtn:false,
		lockScroll:lockedScroll,
		autoPos:{left:'center',top:'center'},
		page:editUrl
	});
	editMess.ShowDialog();
}

/**
 * 删除菜单
 */
var delMenuClick = function() {
	var lockedScroll = false;
	if($.browser.msie){
		lockedScroll = true;
	}
	var delId = "delMessId";
	var index = $(".main > .position > ul > .menu_del > ul a").index(this);
	switch (index) {
	case 1:
		var delMess = new $.dialog( {
			id : delId,
			width : 290,
			height : 110,
			cover : true,
			iconTitle : false,
			xButton : true,
			resize : false,
			rang : true,
			maxBtn : false,
			lockScroll:lockedScroll,
			cancelBtnTxt : '确  认',
			onCancel : function() {
				$('#del').val('all');
				$('#active').val('del');
				$('#listForm').submit();
			},
			title : '确认删除？',
			html : '<p class="mess-detail">此次操作将不能撤消，确认全部删除吗？</p>'
		});
		delMess.ShowDialog();
		break;
	default:
		var delMess = new $.dialog( {
			id : 'subMess',
			width : 335,
			height : 110,
			cover : true,
			iconTitle : false,
			xButton : true,
			resize : false,
			rang : true,
			maxBtn : false,
			lockScroll:lockedScroll,
			cancelBtnTxt : '确  认',
			onCancel : function() {
				$('#del').val('');
				$('#active').val('del');
				$('#listForm').submit();
			},
			title : '确认删除？',
			html : '<p class="mess-detail">此次操作将不能撤消，确认删除所选择的选项吗？</p>'
		});
		delMess.ShowDialog();
		break;
	}
	return false;
}

/**
 * 批量操作
 */
var batchMenuClick = function() {
	var index = $(".main > .position > ul > .menu_move > ul a").index(this);
	var activeValue = '';
	var hiddenName = '';
	var hiddenValue = '';
	switch(index){
		case 0:
			//移动
			activeValue = 'remove';
			var lockedScoll = false;
			if($.browser.msie){
				lockedScoll = true;
			}else if($.browser.mozilla){
				lockedScoll = false;
			}
			//if(selectedIdLength()==1){
			//	$('.moveBox > #' + dialogSelectId + ' option[value="' + $('.id-checkbox:checked').val() + '"]').remove();
			//}
			var moveMess = new $.dialog( {
				id : 'moveMess',
				width : 300,
				height : 102,
				iconTitle : false,
				resize : false,
				cover : true,
				drag : true,
				rang : true,
				maxBtn : false,
				lockScroll : lockedScoll,
				autoPos : {left : 'center',top : 'center'},
				cancelBtnTxt : '移 动',
				onCancel : function() {
					$('#listForm .apptend-container').html('');
					$("#active").val(activeValue);
					$(moveMess.iDG('moveMess')).find('.search-box').appendTo("#listForm .apptend-container");
					$("#listForm").submit();
				},
				title : '移动至',
				html : '<div class="search-box" id="search-box">' + $('.moveBox').html() + '</div>'
			});
			moveMess.ShowDialog();
			return false;
			break;
		case 1:
			//启用
			activeValue = 'disabled';
			hiddenName = 'disabled';
			hiddenValue = '0';
			break;
		case 2:
			//禁用
			activeValue = 'disabled';
			hiddenName = 'disabled';
			hiddenValue = '1';
			break;
		case 3:
			//最新
			activeValue = 'newest';
			hiddenName = 'newest';
			hiddenValue = '1';
			break;
		case 4:
			//取消最新
			activeValue = 'newest';
			hiddenName = 'newest';
			hiddenValue = '0';
			break;
		case 5:
			//置顶
			activeValue = 'top';
			hiddenName = 'top';
			hiddenValue = '1';
			break;
		case 6:
			//取消置顶
			activeValue = 'top';
			hiddenName = 'top';
			hiddenValue = '0';
			break;
		case 7:
			//推荐
			activeValue = 'recommend';
			hiddenName = 'recommend';
			hiddenValue = '1';
			break;
		case 8:
			//取消推荐
			activeValue = 'recommend';
			hiddenName = 'recommend';
			hiddenValue = '0';
			break;
		case 9:
			//首页显示
			activeValue = 'showhome';
			hiddenName = 'showhome';
			hiddenValue = '1';
			break;
		case 10:
			//首页隐藏
			activeValue = 'showhome';
			hiddenName = 'showhome';
			hiddenValue = '0';		
			break;
		case 11:
			activeValue = 'setparam';
			var lockedScoll = false;
			if($.browser.msie){
				lockedScoll = true;
			}else if($.browser.mozilla){
				lockedScoll = false;
			}
			var paramMess = new $.dialog( {
				id : 'paramMess',
				width : 300,
				height : setParamHeight,
				iconTitle : false,
				resize : false,
				cover : true,
				drag : true,
				rang : true,
				maxBtn : false,
				lockScroll : lockedScoll,
				autoPos : {left : 'center',top : 'center'},
				cancelBtnTxt : '设 置',
				onCancel : function() {
					$('#listForm .apptend-container').html('');
					$("#active").val(activeValue);
					$(paramMess.iDG('paramMess')).find('.search-box').appendTo("#listForm .apptend-container");
					$("#listForm").submit();
				},
				title : '批量设置属性',
				html : '<div class="search-box" id="search-box">' + $('.paramBox').html() + '</div>'
			});
			paramMess.ShowDialog();
			return false;
			break;
	}
	$('#active').val(activeValue);
	$(".apptend-container").html('').append('<input type="hidden" name="' + hiddenName + '" id="' + hiddenName + '" value="' + hiddenValue + '" />');
	$('#listForm').submit();
}

/**
 * 单项删除
 * 
 * @param id
 * @return
 */
var delF = function(id) {
	var lockedScroll = false;
	if($.browser.msie){
		lockedScroll = true;
	}
	try {
		editMess.cancel();
	} catch (e) {};
	
	var delMess = new $.dialog( {
		id : 'subMess',
		width : 260,
		height : 110,
		cover : true,
		iconTitle : false,
		xButton : true,
		resize : false,
		rang : true,
		maxBtn : false,
		lockScroll:lockedScroll,
		cancelBtnTxt : '确  认',
		onCancel : function() {
			var delUrl = getInfo + "/del/" + tvName + "/" + id;
			//location.href = delUrl;
			//return false;
			var returnValue = $.ajax({async : false,url : delUrl}).responseText.toLowerCase();
			returnValue = eval(returnValue);
			if (returnValue) {
				location.reload();
			}
		},
		title : '确认删除？',
		html : '<p class="mess-detail">此次操作将不能撤消，确认删除吗？</p>'
	});
	delMess.ShowDialog();
	return false;
}

/**
 * js入口方法
 */
$(function() {
	$(document).bind('contextmenu',function(){return false;}).bind('selectstart',function(){return;});
	var editMess = new Object();
	loadMainMenu();
	$(".main > .position > ul > li").bind('mouseover', menuOver).bind('mouseout', menuOut).bind('click', menuClick);
	$(".main > .list > ul form .item").bind('mouseover', itemOver).bind('mouseout', itemOut).bind('click', itemClick);
	$(".main .id-checkbox").bind('click', idClick);
	$(".main > .list .index-input").bind('change', changeIndex).click(function(event) {
		$(this).select();
		event.stopPropagation();
	});
	$("input[name='all']").bind('click', allClick);
	$(".main > .list .single-menu").hide().find("a").bind('click', singleMenu);
	$('.searchBox').appendTo("#listForm .apptend-container");
});